<?php 

Class wxkeyModel extends AppModel
{
	
	
	public function ordering($val,$id)
	{
		$result=array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('article')." SET ordering='$val' WHERE id='".$id."'";
		$res=$this->query($sql);
		if(!res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
	function del_key($id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
			
				$sql="DELETE FROM ".$this->table('wxkeyword')." WHERE  id='".$id."'";
	
				$res=$this->query($sql);
				 
				if(!$res)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
	
	
		}
			 return $result;
	
	
	}
	public function get_key($id)
	{
		if($id)
		{
		  $sql="SELECT a.*,(SELECT b.name FROM ".$this->table('rule')." as b WHERE a.rid=b.id) as rule_name FROM ".$this->table('wxkeyword')." as a WHERE a.id = '".$id."'";
			
		  $result=$this->getRow($sql);
		  if($result)
		  {
			return $result;
		  }
		  else
		  {
			return $result=array();
		  }
		}
	}
	public function update_key($info,$id)
	{
		if($id)
		{
		
			foreach($info as $k=>$v)
			{
				
					$where.=" $k='$v',";
						
				
			}
			
			$where=substr($where,0,strlen($field)-1);
		
			$sql="UPDATE ".$this->table('wxkeyword')." SET ".$where." WHERE id='".$id."'";
			
			return $res=$this->query($sql);
		}
	}
	public function create_key($info)
	{
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'');
		
		
		
			
			foreach($info as $k=>$v)
			{
				if($v==='')
				{
					
			
				}
				else 
				{
					$field.="$k,";
					
					
					$value.="'$v',";
				}
			}
			
			
			$field=substr($field,0,strlen($field)-1);
			$value=substr($value,0,strlen($value)-1);
			
			$sql="INSERT INTO ".$this->table('wxkeyword')." (".$field.") VALUES(".$value.")";
			if(!$res=$this->query($sql))
			{
				$result['error']=1;
				$result['content']=Lang('SQL_ERROR');
			}
		
	
		return $result;
		//$this->make_thumb('/Uploads/ad/1398673595657549402.jpg','200','200');
	}
	public function key_list($page=1,$sort=array())
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where=' WHERE 1=1';
		if($sort['keyword']!='')
		{
			$where.=" AND keyword like '%".$sort['keyword']."%'";
		}
		$result=$this->show_page("SELECT id,keyword FROM ".$this->table('wxkeyword').$where.' ORDER BY id DESC',$page);
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
		
		
		return $listPage;
	}

	public function check_key($code,$id=0)
	{
		if($id>0)
		{
			$sql="SELECT keyword FROM ".$this->table('wxkeyword')." WHERE id='".$id."'";
			$oldcode=$this->getOne($sql);
			$code_exist=0;
			if($oldcode!=$code)
			{
				$sql="SELECT count(*) FROM ".$this->table('wxkeyword')." WHERE keyword='$code'";
				$code_exist=$this->getOne($sql);
					
			}
			
			if($code_exist>0)
			{
				
				return false;
			}
			else 
			{
				return true;
			}
		}
		else 
		{
		    $sql="SELECT count(*) FROM ".$this->table('wxkeyword')." WHERE keyword='$code'";
		    if($this->getOne($sql)>0)
		   {
			return false;
		   }
		   else 
		  {
			return true;
		  }
		}
	}
	
	
}